<template>
  <el-container>
    <el-main class="main-bg">
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <el-row :gutter="12">
          <el-col :span="6">
            <el-form-item label="库名称">
              <el-select
                v-model="searchForm.warehouseName"
                placeholder="请选择库名称"
              >
                <el-option
                  v-for="item in warehouseList"
                  :key="item.warehouseId"
                  :label="item.warehouseName"
                  :value="item.warehouseName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="商家名称">
              <el-select
                v-model="searchForm.businessName"
                placeholder="请选择商家"
              >
                <el-option
                  v-for="item in selectOpt"
                  :key="item.id"
                  :label="item.businessName"
                  :value="item.businessName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="操作人">
              <el-select
                v-model="searchForm.creator"
                placeholder="请选择操作人"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.userCode"
                  :label="item.userName"
                  :value="item.userName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="抽选时间">
              <el-date-picker
                v-model="createdTime"
                end-placeholder="结束日期"
                placeholder="选择日期"
                range-separator="至"
                start-placeholder="开始日期"
                style="width: 100%"
                type="daterange"
                value-format="yyyy-MM-dd"
                @change="onChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" class="text-right">
            <el-button
              icon="iconfont iconxianxing_sousuo"
              type="primary"
              @click="handleSearch"
            >
              查询
            </el-button>
            <el-button
              icon="iconfont iconxianxing_zhongzhi"
              @click="handleReset"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <Table
        ref="table"
        :current-page="page"
        :header-data="tableHeader"
        :index="false"
        :loading="loading"
        :page-size="limit"
        :table-data="tableData"
        :total="total"
        class="data-table"
        style="width: 100%"
        border
        @now-page="handlePage"
        @now-size="handleSize"
        @table-sort="handleSort"
      />
    </el-main>
  </el-container>
</template>

<script>
import addTable from "@/mixins/addtable";
import selectMinx from "../mixins/index";
import { getExtractList, postExtractDel } from "./api";
import { mapGetters } from "vuex";
import { parseTime } from "@/utils";
export default {
  name: "ExtractList",
  mixins: [addTable, selectMinx],
  data() {
    return {
      searchForm: {
        creator: "",
        businessName: "",
        createdTime: [],
      },
      createdTime: null,
      title: "",
      type: "look",
      tableHeader: [],
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    this.tableHeader = [
      {
        prop: "planName",
        label: "抽选计划名称",
        sortable: false,
        overflow: true,
        align: "left",
      },
      {
        prop: "warehouseName",
        label: "库名称",
        sortable: false,
        overflow: true,
        align: "left",
      },
      {
        prop: "businessName",
        label: "商家名称",
        sortable: false,
        overflow: true,
        align: "left",
      },
      {
        prop: "creator",
        label: "操作人",
        sortable: false,
        overflow: true,
        align: "left",
      },
      {
        prop: "createdTime",
        label: "操作时间",
        sortable: false,
        overflow: true,
        align: "left",
        width: 180,
        render: (h, vm) => {
          const time = vm.createdTime ? parseTime(vm.createdTime) : "";
          return <span>{time}</span>;
        },
      },
      {
        width: 150,
        label: "操作",
        align: "left",
        fixed: "right",
        buttonList: (row, index) => {
          let arr = [];
          const isAdmin = this.userInfo.role === "1";
          if (isAdmin) {
            arr = [
              {
                text: "打印",
                type: "text",
                event: this.onDetail,
              },
              {
                text: "删除",
                type: "text",
                event: this.onDelete,
              },
            ];
          } else {
            arr = [
              {
                text: "打印",
                type: "text",
                event: this.onDetail,
              },
            ];
          }
          return arr;
        },
      },
    ];
    this.getTableData();
  },

  methods: {
    onChange(date) {
      if (date) {
        const dateMin = date[0] + " 00:00:00";
        const dateMax = date[1] + " 23:59:59";
        const timestampMin = new Date(dateMin).getTime();
        const timestampMax = new Date(dateMax).getTime();
        this.searchForm.createdTime = [timestampMin, timestampMax];
      }
    },
    onDetail(row) {
      this.$router.push({
        path: "/extract-detail",
        query: {
          extractId: row.extractId,
        },
      });
    },
    onDelete(row) {
      this.$confirm("您确认要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        postExtractDel({ extractId: row.extractId })
          .then(({ success, message }) => {
            if (success) {
              this.$message.success(message);
            } else {
              this.$message.error(message);
            }
            this.getTableData();
          })
          .catch();
      });
    },
    getTableData() {
      this.loading = true;
      this.getTableParams();
      getExtractList(this.tableParams)
        .then((res) => {
          if (!res) return;
          const { data, pageInfo } = res;
          setTimeout(() => {
            this.loading = false;
            this.tableData = data;
            this.total = pageInfo.total;
          }, 500);
        })
        .catch((err) => {
          console.log("TCL: getTableData -> err", err);
        });
    },
  },
};
</script>
